package com.mqtt.ctwingmqtt.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mqtt.ctwingmqtt.domain.PageResult;
import com.mqtt.ctwingmqtt.domain.dto.DtuDto;
import com.mqtt.ctwingmqtt.domain.entity.DtuEntity;
import com.mqtt.ctwingmqtt.domain.vo.DtuVO;
import com.mqtt.ctwingmqtt.mapper.DtuMapper;
import com.mqtt.ctwingmqtt.service.DtuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DtuServiceImpl implements DtuService {
    @Autowired
    private DtuMapper dtuMapper;

    @Override
    public Boolean add(DtuDto dtuDto) {
        DtuEntity dtuEntity = new DtuEntity();
        BeanUtils.copyProperties(dtuDto, dtuEntity);
        dtuEntity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        dtuEntity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return dtuMapper.insert(dtuEntity) > 0;
    }

    @Override
    public Boolean delete(Long id) {
        return dtuMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean update(DtuDto dtuDto) {
        DtuEntity dtuEntity = new DtuEntity();
        BeanUtils.copyProperties(dtuDto, dtuEntity);
        dtuEntity.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return dtuMapper.updateById(dtuEntity) > 0;
    }

    @Override
    public List<DtuVO> selectAll() {
        List<DtuEntity> dtuEntities = dtuMapper.selectList(null);
        return dtuEntities.stream()
                .map(this::convertToDtuVO)
                .collect(Collectors.toList());
    }

    @Override
    public DtuVO selectOne(Long id) {
        QueryWrapper<DtuEntity> queryWrapper = Wrappers.query();
        queryWrapper.eq("id", id);
        DtuEntity dtuEntity = dtuMapper.selectOne(queryWrapper);
        return convertToDtuVO(dtuEntity);
    }

    @Override
    public PageResult<DtuVO> selectPage(Long pageNum, Long pageSize, String dtuCode, String dtuName, Long dtuId) {
        // 1. 构造分页对象
        Page<DtuEntity> page = new Page<>(pageNum, pageSize);

        // 2. 构造查询条件
        QueryWrapper<DtuEntity> queryWrapper = Wrappers.query();
        if (StringUtils.isNotBlank(dtuCode)) {
            queryWrapper.like("dtu_code", dtuCode);
        }
        if (StringUtils.isNotBlank(dtuName)) {
            queryWrapper.like("dtu_name", dtuName);
        }
        if (dtuId != null) {
            queryWrapper.eq("id", dtuId);
        }

        // 3. 执行分页查询
        Page<DtuEntity> dtuPage = dtuMapper.selectPage(page, queryWrapper);

        // 4. 转换数据为 VO 对象
        List<DtuVO> records = dtuPage.getRecords().stream()
                .map(this::convertToDtuVO)
                .collect(Collectors.toList());

        // 5. 封装分页结果
        PageResult<DtuVO> pageResult = new PageResult<>();
        pageResult.setTotal(dtuPage.getTotal());
        pageResult.setRecords(records);
        pageResult.setPageNum(dtuPage.getCurrent());
        pageResult.setPageSize(dtuPage.getSize());

        return pageResult;
    }

    private DtuVO convertToDtuVO(DtuEntity entity) {
        if (entity == null) {
            return null;
        }
        DtuVO vo = new DtuVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}